package com.sanpu.kxe.business.trade.view;

import android.content.DialogInterface;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.main.bean.UserConfigOutput;
import com.sanpu.kxe.business.main.provider.IMainProvider;
import com.sanpu.kxe.business.trade.adapter.ChargerPageAdapter;
import com.sanpu.kxe.business.trade.bean.TabEntity;
import com.sanpu.kxe.business.trade.contract.TradeConfig;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.module.dialog.GlobalDialog;
import com.sanpu.kxe.module.tablelayout.CommonTabLayout;
import com.sanpu.kxe.module.tablelayout.CustomTabEntity;
import com.sanpu.kxe.module.tablelayout.listener.OnTabSelectListener;
import com.sanpu.kxe.utils.PublicProviderUtil;

import java.util.ArrayList;

import butterknife.Bind;

/**
 * Description：话费流量充值
 * Created by 薛勇军 on 2019/3/12.
 */
@Route(path = "/trade/ChargerTelFee")
public class ChargerTelFeeActivity extends BaseTitleActivity {

    @Bind(R.id.tab_layout)
    CommonTabLayout vTabLayout;
    @Bind(R.id.view_page)
    ViewPager vViewPage;

    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_charger_telfee;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("话费充值");

        UserCenterOutput centerOutput = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (centerOutput != null) {
            final String authenState = centerOutput.getChkSts();
            String remark = null;
            if (TextUtils.equals(CommonConfig.AUTHEN_NOTSUB, authenState)) {
                remark = "请先进行实名认证，再绑卡";
            } else if (TextUtils.equals(CommonConfig.AUTHEN_AUDIT, authenState)) {
                remark = "实名认证正在审核中，请稍候";
            } else if (TextUtils.equals(CommonConfig.AUTHEN_NOPASS, authenState)) {
                remark = "实名认证审核失败，请重新提交审核";
            }

            if (!TextUtils.isEmpty(remark)) {
                GlobalDialog dialog = new GlobalDialog(mContext).content(remark);
                dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        if (!TextUtils.equals(CommonConfig.AUTHEN_AUDIT, authenState))
                            openCertificationActivity();
                        onBackPressed();
                    }
                });
                dialog.show();
            }
        }

        UserConfigOutput configOutput = CacheManage.getInstance().getCache(CacheModel.USER_CONFIG);
        String mobileRechargeTable = TradeConfig.RECHARGE_BALANCE_AND_POINT;
        if (configOutput != null) {
            mobileRechargeTable = configOutput.getMobileRechargeTable();
            switch (mobileRechargeTable) {
                case TradeConfig.RECHARGE_BALANCE:
                case TradeConfig.RECHARGE_POINT:
                    vTabLayout.setVisibility(View.GONE);
                    break;
                default:
                    mTabEntities.add(new TabEntity("金额充值", 0, 0));
                    mTabEntities.add(new TabEntity("积分充值", 0, 0));
                    vTabLayout.setTabData(mTabEntities);
                    vTabLayout.setVisibility(View.VISIBLE);
                    break;
            }
        }
        vViewPage.setAdapter(new ChargerPageAdapter(getSupportFragmentManager(), mobileRechargeTable));
    }

    @Override
    protected void setListener() {
        super.setListener();
        vTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                vViewPage.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
        vViewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                vTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 去认证
     */
    private void openCertificationActivity() {
        IMainProvider mainProvider = PublicProviderUtil.getProvider(IMainProvider.class);
        if (mainProvider != null) {
            mainProvider.openCertificationActivity(mContext, false);
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }
}
